package com.wigem.util;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipUtils {

    /**
     * 删除文件夹
     *
     * @param dir 文件夹
     * @return 是否删除成功
     */
    public static boolean deleteDirectory(File dir) {
        if (dir.isDirectory()) {
            String[] children = dir.list();
            for (String aChildren : children) {
                boolean success = deleteDirectory(new File(dir, aChildren));
                if (!success) {
                    return false;
                }
            }
        }

        // 目录现在为空，可以删除
        return dir.delete();
    }

    public static Boolean createFile(String folderPath) {
        //String folderPath = "/path/to/new/folder";
        File folder = new File(folderPath);
        // 判断文件夹是否存在，存在则删除后重新创建
        boolean result = false;
        if (folder.exists()) {
            deleteDirectory(folder);
        }
        result = folder.mkdirs(); // 创建包括所有必需的父目录
        return result;
    }

    public static void copyFile(File sourceFile, File destinationFile) throws IOException {
        if (!sourceFile.exists()) {
            throw new FileNotFoundException("源文件不存在: " + sourceFile);
        }

        if (!destinationFile.getParentFile().exists()) {
            if (!destinationFile.getParentFile().mkdirs()) {
                throw new IOException("无法创建目标目录: " + destinationFile.getParent());
            }
        }

        // 使用 8KB 缓冲区提升性能
        byte[] buffer = new byte[8192];
        try (FileInputStream fis = new FileInputStream(sourceFile);
             FileOutputStream fos = new FileOutputStream(destinationFile)) {
            int length;
            while ((length = fis.read(buffer)) > 0) {
                fos.write(buffer, 0, length);
            }
        }
    }


    public static void zipFolder(String folderPath, String zipFilePath) throws IOException {
        File folder = new File(folderPath);
        if (!folder.exists() || !folder.isDirectory()) {
            throw new IOException("Folder does not exist or is not a directory.");
        }

        try (FileOutputStream fos = new FileOutputStream(zipFilePath);
             ZipOutputStream zos = new ZipOutputStream(fos)) {

            zipFolderRecursively(folder, folderPath, zos);
        }
    }

    private static void zipFolderRecursively(File folder, String basePath, ZipOutputStream zos) throws IOException {
        File[] files = folder.listFiles();
        if (files == null) {
            return; // Empty directory or I/O error occurred
        }

        for (File file : files) {
            String filePath = file.getPath();
            if (file.isDirectory()) {
                // For directories, we create a new directory entry in the zip file
                zipFolderRecursively(file, basePath, zos);
            } else {
                // For files, we add the file to the zip output stream
                addToZip(file, filePath.substring(basePath.length() + 1), zos);
            }
        }
    }

    private static void addToZip(File file, String zipFilePath, ZipOutputStream zos) throws IOException {
        try (FileInputStream fis = new FileInputStream(file)) {
            ZipEntry zipEntry = new ZipEntry(zipFilePath);
            zos.putNextEntry(zipEntry);

            byte[] buffer = new byte[1024];
            int length;
            while ((length = fis.read(buffer)) > 0) {
                zos.write(buffer, 0, length);
            }

            zos.closeEntry();
        }
    }
}
